﻿Public Class Details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Count_user As Integer = validateUser()
        Ratingcount()
        If Count_user = 1 Then
            rbl_rating.Visible = False
            btn_submit_rating.Visible = False
            yourrating()
        Else
            rbl_rating.Visible = True
            btn_submit_rating.Visible = True
        End If

        img_like.Visible = False
        img_unlike.Visible = False
        lbl_like_unlike_message.Visible = False
        RadioButtonList1.Visible = False
        btn_submit.Visible = False
        Dim UserLike_Unlike As Integer = Thumps_UP_Down()
        Dim Record_Count As Integer = Count()

        If Record_Count = 0 Then
            lbl_like_unlike_message.Visible = True
            lbl_like_unlike_message.Text = " Like? Or UnLike? "
            btn_submit.Visible = True
            RadioButtonList1.Visible = True
            img_like.Visible = False
            img_unlike.Visible = False
        ElseIf UserLike_Unlike = 1 Then
            img_like.Visible = True
            lbl_like_unlike_message.Visible = True
            lbl_like_unlike_message.Text = " This Book is Worth To read"
            img_unlike.Visible = False
            btn_submit.Visible = False
            RadioButtonList1.Visible = False
        ElseIf UserLike_Unlike = 0 Then
            img_unlike.Visible = True
            lbl_like_unlike_message.Visible = True
            lbl_like_unlike_message.Text = " Not That Much Interest "
            btn_submit.Visible = False
            RadioButtonList1.Visible = False
            img_like.Visible = False
        Else
            img_like.Visible = False
            img_unlike.Visible = False
            lbl_like_unlike_message.Visible = False
            btn_submit.Visible = False
            RadioButtonList1.Visible = False
        End If
    End Sub

    Protected Sub btn_BackToList_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_BackToList.Click
        Response.Redirect("List.aspx")
    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim BID As Integer = Request.QueryString("ID")
        Dim sql As String = " INSERT INTO Books_Likes (BL_BookID, BL_UserID, Like_Unlike) VALUES (@v1,@v2,@v3)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v1", BID)
        cmd.Parameters.AddWithValue("@v2", Uid)
        cmd.Parameters.AddWithValue("@v3", RadioButtonList1.SelectedValue)
        conn.Open()
        Try
            If RadioButtonList1.SelectedItem.Selected = True Then
                cmd.ExecuteNonQuery()
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            Else
                lbl_error.Text = " *Please Select either Like Or UnLike."
            End If
        Catch
        End Try
        Response.Redirect("Details.aspx?ID=" & BID & "")
    End Sub
    Protected Function Thumps_UP_Down() As Integer
        Dim U_id As Integer
        Dim Uname As String = Page.User.Identity.Name
        Dim ULike As Integer
        U_id = CInt(userID(Uname))
        Dim BID As Integer = CInt(Request.QueryString("ID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select  Like_Unlike from Books_Likes Where BL_UserID =" & U_id & " AND BL_BookID = " & BID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            ULike = CInt(Dreader("Like_Unlike"))

        Catch

        End Try
        Return ULike
    End Function
    Protected Function userID(ByVal uname As String) As Integer
        Dim uID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select aspnet_Users.UserId from aspnet_Users Where aspnet_Users.UserName = '" & uname & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            uID = Dreader("UserId")
        Catch
        End Try
        Return uID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()
    End Function
    Protected Function Count() As Integer
        Dim U_id As Integer
        Dim Uname As String = Page.User.Identity.Name
        U_id = CInt(userID(Uname))
        Dim BID As Integer = CInt(Request.QueryString("ID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select Count(*) As RowCount from Books_Likes Where BL_UserID =" & U_id & " AND BL_BookID = " & BID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dreader = IDcmd.ExecuteReader()
        Dreader.Read()
        Dim ULike_Count As Integer = CInt(Dreader("RowCount"))
        Return ULike_Count
    End Function

    Protected Sub btn_submit_rating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit_rating.Click
        Dim B_ID As Integer = Request.QueryString("ID")
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim sql As String = " INSERT INTO BooksRating (BR_BookID, BR_Rating, BR_UserID) VALUES (@v1, @v2, @v3)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v1", B_ID)
        cmd.Parameters.AddWithValue("@v2", rbl_rating.SelectedValue)
        cmd.Parameters.AddWithValue("@v3", Uid)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("Details.aspx?ID=" & B_ID)
    End Sub
    Protected Sub Ratingcount()
        Dim BookID As Integer = CInt(Request.QueryString("ID"))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select Count(*) AS CountR, Avg(BR_Rating) AS BRating from BooksRating Where BR_BookID = " & BookID & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            lbl_rating_Users.Text = CStr(Dreader("CountR"))
            lbl_Avg_Rating.Text = CStr(Dreader("BRating"))
        Catch
        End Try
    End Sub
    Protected Sub yourrating()
        Dim BookID As Integer = CInt(Request.QueryString("ID"))
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select BR_Rating from BooksRating Where Br_BookID = " & BookID & " And BR_UserID = " & Uid & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dreader = IDcmd.ExecuteReader()
        Dreader.Read()
        lbl_r_by_u.Text = "Your Rating: " + CStr(Dreader("BR_Rating")) + "<br /> Thank you"

    End Sub
    Protected Function validateUser() As Integer
        Dim BookID As Integer = CInt(Request.QueryString("ID"))
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select Count(*) AS Count_User from BooksRating Where BR_BookID = " & BookID & " AND BR_UserID = " & Uid & ""
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Dreader = IDcmd.ExecuteReader()
        Dreader.Read()
        Dim C_Count As Integer = Dreader("Count_User")
        Return C_Count
    End Function
End Class